package webs;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.pojo.News;
import domain.ApplicationContext;
import domain.NewsFacade;

public class GetNews extends HttpServlet{
	
	    private NewsFacade newsService;
	
	    @Override
	    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
	        doPost(httpServletRequest,httpServletResponse);
	    }

	    @Override
	    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
	    	
	    	httpServletRequest.setCharacterEncoding("UTF-8");
	    	HttpSession httpsession = httpServletRequest.getSession();
	    	NewsFacade newsService = (NewsFacade)ApplicationContext.get("NewsService");
	    	
	    	Integer news_id = new Integer(httpServletRequest.getParameter("news_id"));
	    	News temp_news = new News();
	    	temp_news.setId(news_id);
	    	News db_news = newsService.getNews(temp_news);
	    	httpsession.setAttribute("news", db_news);
	    	httpServletResponse.sendRedirect("news.jsp");
	    }

	    @Override
	    public void destroy() {
	        super.destroy();    //To change body of overridden methods use File | Settings | File Templates.
	    }

	    @Override
	    public void init() throws ServletException {
	        super.init();    //To change body of overridden methods use File | Settings | File Templates.
	    }

		public NewsFacade getNewsService() {
			return newsService;
		}

		public void setNewsService(NewsFacade newsService) {
			this.newsService = newsService;
		}
	}
